﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DreamBox.Admin.File
{
    public partial class FileUpdate : BLL.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DAL.PSelect ps = new DAL.PSelect();
                drop_UrlClass.DataSource = ps.Select("dbox_FileClass", "*", "");
                drop_UrlClass.DataTextField = "CName";
                drop_UrlClass.DataValueField = "CID";
                drop_UrlClass.DataBind();

                //加载现有传递过来的数据
                if (Request.QueryString["CID"] != null)
                {
                    DataTable dt = ps.Select("dbox_FileContents", "*", "FileID=" + Request.QueryString["FileID"]);

                    tbox_Title.Text = dt.Rows[0]["Title"].ToString();
                    //tbox_Url.Text = dt.Rows[0]["Url"].ToString();
                    Contents.Text = dt.Rows[0]["Contents"].ToString();
                    tbox_Sort.Text = dt.Rows[0]["Sort"].ToString();

                    cbox_IsStatus.Checked = bool.Parse(dt.Rows[0]["IsStatus"].ToString());

                    foreach (ListItem item in drop_UrlClass.Items)
                    {
                        if (item.Value == dt.Rows[0]["CID"].ToString())
                        {
                            item.Selected = true;
                        }
                    }
                }
            }
        }

        protected void btn_Post_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["FileID"] != null)
            {
                Model.FileContents m = new Model.FileContents();
                m.FileID = int.Parse(Request.QueryString["FileID"]);
                m.CID = int.Parse(drop_UrlClass.SelectedValue);
                m.Title = tbox_Title.Text.Trim();
                //m.Url = tbox_Url.Text.Trim();
                m.Contents = Contents.Text.Trim();
                m.Sort = int.Parse(tbox_Sort.Text.Trim());
                m.IsStatus = cbox_IsStatus.Checked;
                DAL.MAction action = new DAL.MAction();
                if (action.Update(m))
                {
                    BLL.Admin.Tips.DetailedTips(this, "操作成功", "/File/FileList.aspx", "1");
                }
            }
        }
    }
}